package chch;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class SmallPanel extends JPanel {

	// KOMENTAR

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BufferedImage image;
	private String pinyin;
	private boolean flipped;

	public SmallPanel(String imgUrl, String pinyin) {

		setPinyin(pinyin);
		setFlipped(true);

		try {
			setImage(ImageIO.read(new File(imgUrl)));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));

		addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent arg0) {

				flip();
				repaint();

			}

		});
	}

	
	void flip() {

		flipped = !flipped;

	}

	public void drawBorder(Graphics arg0, Color color) {

		Color old = arg0.getColor();
		arg0.setColor(color);
		arg0.drawLine(0, 0, 0, image.getHeight());
		arg0.drawLine(0, 0, image.getWidth(), 0);
		arg0.drawLine(image.getWidth(), image.getHeight(), 0, image.getHeight());
		arg0.drawLine(image.getWidth(), image.getHeight(), image.getWidth(), 0);
		arg0.setColor(old);

	}

	@Override
	protected void paintComponent(Graphics arg0) {
		// TODO Auto-generated method stub
		super.paintComponent(arg0);
		if (flipped) {
			arg0.drawString(pinyin, 50, 50);
			drawBorder(arg0, Color.DARK_GRAY);
		} else {
			arg0.drawImage(image, 0, 0, null);
			drawBorder(arg0, Color.LIGHT_GRAY);
		}

	}

	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}

	public String getPinyin() {
		return pinyin;
	}

	public void setPinyin(String pinyin) {
		this.pinyin = pinyin;
	}

	public boolean isFlipped() {
		return flipped;
	}

	public void setFlipped(boolean flipped) {
		this.flipped = flipped;
	}
}
